package com.zoe.onelink.actuate.check;

import cn.hutool.http.HttpUtil;
import com.zoe.onelink.plugin.check.AbstractEnvironmentChecker;
import com.zoe.onelink.plugin.check.model.PropertyCheckResult;
import com.zoe.onelink.util.StrUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-11-24
 */
public class ZipkinEnvironmentChecker extends AbstractEnvironmentChecker {


    private static final String ZIPKIN_BASE_URL = "spring.zipkin.base-url";
    private static final String ZIPKIN_SENDER_TYPE_KEY = "spring.zipkin.sender.type";
    private static final String ZIPKIN_SENDER_TYPE_WEB = "web";

    @Override
    protected List<PropertyCheckResult<?>> doCheck() {

        List<PropertyCheckResult<?>> propertyCheckResults = new ArrayList<>();
        String senderType = this.getProperty(ZIPKIN_SENDER_TYPE_KEY);
        if (ZIPKIN_SENDER_TYPE_WEB.equalsIgnoreCase(senderType)) {
            String baseUrl = this.getProperty(ZIPKIN_BASE_URL);
            if (StrUtil.isBlank(baseUrl)) {
                PropertyCheckResult<String> checkResult = new PropertyCheckResult<String>()
                        .setPropKey(ZIPKIN_BASE_URL)
                        .setPropValue(null)
                        .setLevel(PropertyCheckResult.Level.DANGER)
                        .setMessage("Zipkin服务地址未配置");
                propertyCheckResults.add(checkResult);
            } else {
                try {
                    HttpUtil.createGet(baseUrl).timeout(1000).execute();
                } catch (Exception e) {
                    PropertyCheckResult<String> baseUrlPropertyCheckResult = new PropertyCheckResult<String>()
                            .setPropKey(ZIPKIN_BASE_URL)
                            .setPropValue(baseUrl)
                            .setLevel(PropertyCheckResult.Level.DANGER)
                            .setMessage("Zipkin服务地址无法连接");
                    propertyCheckResults.add(baseUrlPropertyCheckResult);
                }
            }
        }

        return propertyCheckResults;
    }
}
